package com.card.manager.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.card.framework.page.model.PageModel;
import com.card.framework.util.CommonUtil;
import com.card.manager.domain.Mad;
import com.card.manager.services.MadService;


@Controller
@RequestMapping("/mad")
public class MadController {
	final static Logger loger = Logger.getLogger(MadController.class);
	@Autowired(required=true) 
	private MadService madServices;
	@InitBinder
	public void setupDataBinder(WebDataBinder dataBinder) {
		//表单页面如果有时间类型的需要initbinder
		dataBinder.registerCustomEditor(String.class,new StringTrimmerEditor(false));  
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	@RequestMapping("/index")
	public String index(){
		return "madList";
	}
	
	
	@RequestMapping("/madUpdateWindow")
	public String madUpdateWindow(HttpServletRequest request) {
		String id = request.getParameter("id");
		Mad mad = null;
		if(StringUtils.isNotEmpty(id)){
			mad = madServices.getById(Long.parseLong(id));
		}else{
			mad = new Mad();
			mad.setCreatdatetime(new Date());
			SecurityContext sc = (SecurityContext)request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
			mad.setCreatuserid(sc.getAuthentication().getName());
		}
		request.setAttribute("mad", mad);
		return "madPopWindow";
	}
	@RequestMapping("/madAddWindow")
	public String madAddWindow(HttpServletRequest request) {
		String id = request.getParameter("id");
		Mad mad = null;
		if(StringUtils.isNotEmpty(id)){
			mad = madServices.getById(Long.parseLong(id));
		}else{
			mad = new Mad();
			mad.setCreatdatetime(new Date());
			SecurityContext sc = (SecurityContext)request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
			mad.setCreatuserid(sc.getAuthentication().getName());
		}
		request.setAttribute("mad", mad);
		return "madPopWindow";
	}
	
	@RequestMapping(value="/queryList",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryList(Mad mad,PageModel pageModel){
		return madServices.selectMadForPage(mad, pageModel);
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> delete(@RequestParam("json") String json){
		Map<String, String> map = new HashMap<String, String>();
		try {
			madServices.deleteMads(CommonUtil.getBean(json, Mad.class));
			map.put("mes", "删除成功");
		} catch (Throwable e) {
			CommonUtil.throwsException(map, e);
			loger.error("delete", e);
		}
		return map;//重定向
	}
	
	@RequestMapping(value="/madAdd",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> madAdd(Mad mad){
		Map<String, String> map = new HashMap<String, String>();
		try {
			madServices.addOrUpdate(mad);
			map.put("mes", "操作成功");
		}
		catch (Throwable e) {
			CommonUtil.throwsException(map, e);
			loger.error("addOrUpdate", e);
		}
		return map; 
	}
	@RequestMapping("/listMad")
	public String listMad(HttpServletRequest request){
		request.setAttribute("currDate",new Date());
		return "listMadPage";
	}
	@RequestMapping("countCurrMad")
	@ResponseBody
	public Map<String,Object> countCurrMad(HttpServletRequest request){
		Map<String,Object> map = new HashMap<String,Object>();
		Mad mad = new Mad();
		Date d = null;
		System.out.println(request.getSession().getLastAccessedTime() + "---------------------------");;
		
		try {
			d = DateUtils.parseDate(DateFormatUtils.format(new Date(), "yyyy-MM-dd"), new String[]{"yyyy-MM-dd"});
		} catch (ParseException e) {
			d = new Date();
		}
		mad.setStartdate(d);
		mad.setEnddate(d);
		map.put("madCount",madServices.count(mad));
		return map;
	}
	@RequestMapping(value="/queryMad",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> listMad(Mad mad,PageModel pageModel){
		return madServices.selectMadForPage(mad, pageModel);
	}
	@RequestMapping("/viewMad")
	@ResponseBody
	public Mad viewMad(@RequestParam("id")Long id){
		Mad mad = madServices.getById(id);
		return mad;
	}
	@RequestMapping(value="/madUpdate",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> madUpdate(Mad mad){
		Map<String, String> map = new HashMap<String, String>();
		try {
			madServices.addOrUpdate(mad);
			map.put("mes", "操作成功");
		}
		catch (Throwable e) {
			CommonUtil.throwsException(map, e);
			loger.error("addOrUpdate", e);
		}
		return map; 
	}
}